<?php
namespace App\Libs\Common;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 *   京东支付
 */
class JdPayController extends Controller
{	
	//支付
	public function pay($tradeNum,$tradeName,$amount,$userId)
	{
       vendor("JdPay.action.ClientOrder");

        $pay = new \ClientOrder();
		
       	//交易流水号(分期表分期ID)
		$param["tradeNum"]= $tradeNum;           //交易号
		$param["tradeName"]=$tradeName;        //交易名称
		$param["amount"]=$amount;             //金额
		//用户唯一标识
		$param["userId"]=$userId;

		$pay->execute($param);
	}

    
    /**
     * 支付完成后同步地址
     * @return [type] [description]
     */
    public function callBack()
    {

        header("Content-type: text/html; charset=utf-8");
        vendor("JdPay.action.CallBack");

         $request=I();

         $obj = new \CallBack();

        $arr=$obj->execute($request);

        if ($arr !=false) 
        {
            $money=$arr['amount']/100;

            $res=save_order_stages($arr['tradeNum'],$money);
            
            if ($res) 
            {
                $order_id=M('order_stages')->where("id='$arr[tradeNum]' ")->getField('order_id');
                //如果log表有记录 并且不是 2 的状态  就删除此条记录
                M("pay_dk_bill")->where("stages_id='$arr[tradeNum]' and dk_status in(0,1)")->delete();

                header("location:https://n.lvyuecp.com/wapJd/my.html");
            }
            
        }else{
            file_put_contents(time().'.txt','京东支付验签失败');
        }

        
         //file_put_contents('同步'.time().'.txt','同步'.json_encode($request));
    }


    
    /**
     *  支付完成后异步通知地址
     */
    public function notify()
    {
    	 $xml=file_get_contents("php://input");  

         vendor("JdPay.action.AsynNotifyAction");

         $obj = new \AsynNotifyAction();

         /*
         $xml='<?xml version="1.0" encoding="UTF-8" ?>
                <jdpay>
                  <version>V2.0</version>
                  <merchant>110891075002</merchant>
                  <result>
                    <code>000000</code>
                    <desc>success</desc>
                  </result>
                  <encrypt>Y2UxMTE3NWQxMTY2YzVlOTU2MmVhZjZlYjAzMmQyZjc4ZTgwOWVmNzkxNjU1Y2M3NWVkOTMyNjYwYWJlYWVkYmEzYTVhOTA2OGE1NTM0NzEyNGVjYjYzM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MTkwMGUyOTNiYjIwZTRhMmNjNDAyMjkyZjkzNjg0ZDkxMDM4YjFjMGM4MGJjOGEzYTU0MDJmODc4NWI2NmZmZWNmYzcyZTMyMzdkNDI3ODcyYzRiN2E3OThmMDI0OTcyMTU5NjExOWJjNzIzNjhmZGUwZWJiYTNlNzI4MGRiM2E4Mzc4YzYzMjU1NGUyMmI4YzUwMmUyNDBlYjgyNzQ2NTYzOQ==</encrypt>
                </jdpay>';
              */

        $data=$obj->execute($xml);

        if ($data !==false) 
        {
            
            //$data['tradeNum']   订单号
            //$data['amount']     金额(单位分)
            //$data['status']     交易状态码 2-成功
            
           print_r($data);

           return "ok";
        }
    	 //file_put_contents(time().'.txt','异步'.$xml);
    }







		
	
}